using System;
using System.Collections.Generic;
using System.Xml;

using ZoneFiveSoftware.Common.Data.Fitness;

namespace BENBUN.ExportCRS
{
    class CredentialsCache
    {
        public static CredentialsCache Instance
        {
            get
            {
                if (instance == null) instance = new CredentialsCache();
                return instance; 
            }
        }

        public Credentials GetCredentials(ILogbook logbook)
        {
            if (cache.ContainsKey(logbook.ReferenceId))
            {
                return cache[logbook.ReferenceId];
            }
            return null;
        }

        public void SetCredentials(Credentials credentials, ILogbook logbook)
        {
            cache.Remove(logbook.ReferenceId);
            if (credentials != null)
            {
                cache.Add(logbook.ReferenceId, credentials);
                _summitSizeAsc = credentials.SummitSizeAsc;
                _valleySizeAsc = credentials.ValleySizeAsc;
                _summitSizeDec = credentials.SummitSizeDec;
                _valleySizeDec = credentials.ValleySizeDec;
                _turnAngle = credentials.TurnAngle;
                _nameSourceLocation = credentials.NameSourceLocation;
                _doSummits = credentials.DoSummits;
                _doValleys = credentials.DoValleys;
                _doTurns = credentials.DoTurns;
                _doMaxLength = credentials.DoMaxLength;
                _distanceUnits = credentials.DistanceUnits;
                _maxLength = credentials.MaxLength;


            }
        }

        internal void ReadOptions(XmlDocument xmlDoc, XmlNamespaceManager nsmgr, XmlElement pluginNode)
        {
            string attr;
            attr = pluginNode.GetAttribute("summitSizeAsc");
            if (attr.Length > 0) _summitSizeAsc = XmlConvert.ToDouble(attr);
            
            attr = pluginNode.GetAttribute("summitSizeDec");
            if (attr.Length > 0) _summitSizeDec = XmlConvert.ToDouble(attr);

            attr = pluginNode.GetAttribute("valleySizeAsc");
            if (attr.Length > 0) _valleySizeAsc = XmlConvert.ToDouble(attr);
            attr = pluginNode.GetAttribute("valleySizeDec");
            if (attr.Length > 0) _valleySizeDec = XmlConvert.ToDouble(attr);

            attr = pluginNode.GetAttribute("turnAngle");
            if (attr.Length > 0) _turnAngle = XmlConvert.ToDouble(attr);

            attr = pluginNode.GetAttribute("maxLength");
            if (attr.Length > 0) _maxLength = XmlConvert.ToDouble(attr);

            attr = pluginNode.GetAttribute("doTurns").ToLower();
            if (attr.Length > 0) _doTurns = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute("doValleys").ToLower();
            if (attr.Length > 0) _doValleys = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute("doSummits").ToLower();
            if (attr.Length > 0) _doSummits = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute("doMaxLength").ToLower();
            if (attr.Length > 0) _doMaxLength = XmlConvert.ToBoolean(attr);

            attr = pluginNode.GetAttribute("nameSourceLocation").ToLower();
            if (attr.Length > 0) _nameSourceLocation = attr;

            attr = pluginNode.GetAttribute("distanceUnits").ToLower();
            if (attr.Length > 0) _distanceUnits = attr;

            attr = pluginNode.GetAttribute("originalTIme").ToLower();
            if (attr.Length > 0) _originalTime = XmlConvert.ToBoolean(attr);

            attr = pluginNode.GetAttribute("newSpeedValue").ToLower();
            if (attr.Length > 0) _newSpeedValue = XmlConvert.ToDouble(attr);

            attr = pluginNode.GetAttribute("newSpeedUnits").ToLower();
            if (attr.Length > 0) _newSpeedUnits = attr;

            BenbunPlugin.Instance.turnAngle = _turnAngle;
            BenbunPlugin.Instance.summitSizeAsc = _summitSizeAsc;
            BenbunPlugin.Instance.summitSizeDec = _summitSizeDec;
            BenbunPlugin.Instance.valleySizeAsc = _valleySizeAsc;
            BenbunPlugin.Instance.valleySizeDec = _valleySizeDec;

            BenbunPlugin.Instance.doTurns = _doTurns;
            BenbunPlugin.Instance.doValleys = _doValleys;
            BenbunPlugin.Instance.doSummits = _doSummits;
            BenbunPlugin.Instance.doMaxLength = _doMaxLength;
            BenbunPlugin.Instance.maxLength = _maxLength;
            BenbunPlugin.Instance.nameSourceLocation = _nameSourceLocation;
            BenbunPlugin.Instance.distanceUnits = _distanceUnits;
            BenbunPlugin.Instance.originalTime = _originalTime;
            BenbunPlugin.Instance.newSpeedUnits = _newSpeedUnits;
            BenbunPlugin.Instance.newSpeedValue = _newSpeedValue;

            
           

        }

        internal void WriteOptions(XmlDocument xmlDoc, XmlElement pluginNode)
        {
            pluginNode.SetAttribute("summitSizeAsc", BenbunPlugin.Instance.summitSizeAsc.ToString());
            pluginNode.SetAttribute("summitSizeDec", BenbunPlugin.Instance.summitSizeDec.ToString());
            pluginNode.SetAttribute("valleySizeDec", BenbunPlugin.Instance.valleySizeDec.ToString());
            pluginNode.SetAttribute("valleySizeAsc", BenbunPlugin.Instance.valleySizeAsc.ToString());
            pluginNode.SetAttribute("turnAngle", BenbunPlugin.Instance.turnAngle.ToString());
            pluginNode.SetAttribute("maxLength", BenbunPlugin.Instance.maxLength.ToString());


            pluginNode.SetAttribute("doTurns", BenbunPlugin.Instance.doTurns.ToString());
            pluginNode.SetAttribute("doValleys", BenbunPlugin.Instance.doValleys.ToString());
            pluginNode.SetAttribute("doSummits", BenbunPlugin.Instance.doSummits.ToString());
            pluginNode.SetAttribute("doMaxLength", BenbunPlugin.Instance.doMaxLength.ToString());
            pluginNode.SetAttribute("nameSourceLocation", BenbunPlugin.Instance.nameSourceLocation);
            pluginNode.SetAttribute("distanceUnits", BenbunPlugin.Instance.distanceUnits);

            pluginNode.SetAttribute("originalTime", BenbunPlugin.Instance.originalTime.ToString());
            pluginNode.SetAttribute("newSpeedValue", BenbunPlugin.Instance.newSpeedValue.ToString());
            pluginNode.SetAttribute("newSpeedUnits", BenbunPlugin.Instance.newSpeedUnits);

            
        }

        private CredentialsCache()
        {
        }

        private static CredentialsCache instance;

        private Double _summitSizeAsc = 1;
        private Double _valleySizeAsc = 1;
        private Double _summitSizeDec = 1;
        private Double _valleySizeDec = 1;
        private Double _turnAngle = 20;
        private Double _maxLength = 20;
        private Boolean _doTurns = true;
        private Boolean _doValleys = true;
        private Boolean _doSummits = true;
        private Boolean _doMaxLength = false;
        private String _nameSourceLocation = "location";
        private String _distanceUnits = "meters";

        private Boolean _originalTime = false;
        private Double _newSpeedValue = 8;
        private String _newSpeedUnits = "MintuesPerMile";

       
        private IDictionary<string, Credentials> cache = new Dictionary<string, Credentials>();
    }
}
